package com.cjx.stars.aop;

import cn.hutool.bloomfilter.BloomFilter;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.cjx.stars.constant.BloomFilterConstants.*;

/**
 * @author cjx
 * @description 相关元素新增时将其加入布隆过滤器
 * @createDate 2024/4/23 17:33
 **/
@Aspect
@Log4j2
@Component
@ConditionalOnProperty(prefix = "stars", name = "enable-bloom-filter", havingValue = "true")
public class InsertBloomFilterAop {

    @Resource
    private BloomFilter bloomFilter;

    /**
     * 新增用户后
     * @param ret 用户id
     */
    @AfterReturning(value = "execution(* com.cjx.stars.service.impl.UserServiceImpl.createRegisterUser(..))", returning = "ret")
    public void afterRegister(Object ret){
        log.info("用户 {} 加入到布隆过滤器中",ret);
        bloomFilter.add(USER_BLOOM_PREFIX + ret);
    }

    /**
     * 新增团队后
     * @param ret 团队id
     */
    @AfterReturning(value = "execution(* com.cjx.stars.service.impl.TeamServiceImpl.insertTeam(..))", returning = "ret")
    public void afterAddTeam(Object ret){
        log.info("团队 {} 加入到布隆过滤器中",ret);
        bloomFilter.add(TEAM_BLOOM_PREFIX + ret);
    }

    /**
     * 新增博文后
     * @param ret 博文id
     */
    @AfterReturning(value = "execution(* com.cjx.stars.service.impl.BlogServiceImpl.insertBlog(..))", returning = "ret")
    public void afterAddBlog(Object ret){
        log.info("博文 {} 加入到布隆过滤器中",ret);
        bloomFilter.add(BLOG_BLOOM_PREFIX + ret);
    }



}
